package com.neusoft.zqygbc.neusoft_wenda.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.fragment.FindFragment;
import com.neusoft.zqygbc.neusoft_wenda.fragment.HomeFragment;
import com.neusoft.zqygbc.neusoft_wenda.fragment.LetterFragment;
import com.neusoft.zqygbc.neusoft_wenda.fragment.MoreFragment;
import com.neusoft.zqygbc.neusoft_wenda.fragment.NoticeFragment;
import com.neusoft.zqygbc.neusoft_wenda.utils.BaiduPushUtils;

public class MainActivity extends AppCompatActivity implements OnClickListener {
    private LinearLayout mTabHome;
    private LinearLayout mTabFind;
    private LinearLayout mTabNotice;
    private LinearLayout mTabLetter;
    private LinearLayout mTabMore;

    private ImageButton mImgHome;
    private ImageButton mImgFind;
    private ImageButton mImgNotice;
    private ImageButton mImgLetter;
    private ImageButton mImgMore;

    private TextView mTextHome;
    private TextView mTextFind;
    private TextView mTextNotice;
    private TextView mTextLetter;
    private TextView mTextMore;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;

    private int press;
    private int normal;

    private int fragid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        LinearLayoutManager LL = new LinearLayoutManager(this);
        initView();//初始化
        initEvent();//初始化事件
        setSelect(0);//
        //接收intent传过来的值，然后进行判断跳转到指定的fragment里面
        Intent intent=getIntent();
        fragid=intent.getIntExtra("fragid",0);
        switch ( fragid){
            case 0:
            { setSelect(0);
                break;}
            case 2:
            {
                setSelect(2);
                break;}
            case 3:
            {
                setSelect(3);
                break;
            }
            case 4:
            {
                setSelect(4);
                break;
            }

        }


    }
    private void initEvent()
    {
        mTabHome.setOnClickListener(this);
        mTabFind.setOnClickListener(this);
        mTabNotice.setOnClickListener(this);
        mTabLetter.setOnClickListener(this);
        mTabMore.setOnClickListener(this);
    }

    private void initView()
    {
        mTabHome = (LinearLayout) findViewById(R.id.id_tab_home);
        mTabFind = (LinearLayout) findViewById(R.id.id_tab_find);
        mTabNotice = (LinearLayout) findViewById(R.id.id_tab_notice);
        mTabLetter = (LinearLayout) findViewById(R.id.id_tab_letter);
        mTabMore = (LinearLayout) findViewById(R.id.id_tab_more);

        mImgHome = (ImageButton) findViewById(R.id.id_tab_home_img);
        mImgFind = (ImageButton) findViewById(R.id.id_tab_find_img);
        mImgNotice = (ImageButton) findViewById(R.id.id_tab_notice_img);
        mImgLetter = (ImageButton) findViewById(R.id.id_tab_letter_img);
        mImgMore = (ImageButton) findViewById(R.id.id_tab_more_img);

        mTextHome= (TextView) findViewById(R.id.id_tab_home_sy);
        mTextFind= (TextView) findViewById(R.id.id_tab_find_fx);
        mTextNotice= (TextView) findViewById(R.id.id_tab_notice_tz);
        mTextLetter= (TextView) findViewById(R.id.id_tab_letter_sx);
        mTextMore= (TextView) findViewById(R.id.id_tab_more_gd);

        press = getResources().getColor(R.color.themecolor);
        normal = getResources().getColor(R.color.normal);
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new HomeFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                mImgHome.setImageResource(R.drawable.ic_home_press);
                mTextHome.setTextColor(press);
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new FindFragment()
                    ;transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);

                }
                mImgFind.setImageResource(R.drawable.ic_faxian_press);
                mTextFind.setTextColor(press);
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new NoticeFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);
                }
                mImgNotice.setImageResource(R.drawable.ic_notice_press);
                mTextNotice.setTextColor(press);
                break;
            case 3:
                if (mTab04 == null)
                {
                    mTab04 = new LetterFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    transaction.show(mTab04);
                }
                mImgLetter.setImageResource(R.drawable.ic_letter_press);
                mTextLetter.setTextColor(press);
                break;
            case 4:
                if (mTab05 == null)
                {
                    mTab05 = new MoreFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else
                {
                    transaction.show(mTab05);
                }
                mImgMore.setImageResource(R.drawable.ic_more_press);
                mTextMore.setTextColor(press);
                break;
            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_tab_home:
                setSelect(0);
                break;
            case R.id.id_tab_find:
                setSelect(1);
                break;
            case R.id.id_tab_notice:
                setSelect(2);
                break;
            case R.id.id_tab_letter:
                setSelect(3);
                break;
            case R.id.id_tab_more:
                setSelect(4);
                break;

            default:
                break;
        }
    }


    private void resetImgs()
    {
        mTextHome.setTextColor(normal);
        mTextFind.setTextColor(normal);
        mTextNotice.setTextColor(normal);
        mTextLetter.setTextColor(normal);
        mTextMore.setTextColor(normal);

        mImgHome.setImageResource(R.drawable.ic_home_normal);
        mImgFind.setImageResource(R.drawable.ic_faxian_normal);
        mImgNotice.setImageResource(R.drawable.ic_notice_normal);
        mImgLetter.setImageResource(R.drawable.ic_letter_normal);
        mImgMore.setImageResource(R.drawable.ic_more_normal);
    }

    // 定义一个变量，来标识是否退出
    private static boolean isExit = false;

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    /**
     * 处理返回键事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }


    @Override
    protected void onNewIntent(Intent intent) {
        String action = intent.getAction();

        if (BaiduPushUtils.ACTION_LOGIN.equals(action)) {
            // Push: 百度账号初始化，用access token绑定
            String accessToken = intent
                    .getStringExtra(BaiduPushUtils.EXTRA_ACCESS_TOKEN);
            PushManager.startWork(getApplicationContext(),
                    PushConstants.LOGIN_TYPE_ACCESS_TOKEN, accessToken);
        }
    }



}
